package com.lz.ionicbase.ui.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lz.ionicbase.R;
import com.lz.ionicbase.SuperApp;
import com.lz.ionicbase.engine.callback.AppCallback;
import com.lz.ionicbase.model.VcodeReturnBean;
import com.lz.ionicbase.utils.BaseUtil;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;
import com.lz.ionicbase.utils.ToastUtils;
import com.lz.ionicbase.view.Mycode;
import com.lzy.okgo.OkGo;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by lenovo on 2017/2/20.
 */

public class PsdInputCodeActivity extends BaseActivity {
    @InjectView(R.id.my_back)
    ImageView myBack;
    @InjectView(R.id.psd_next)
    TextView psdNext;
    @InjectView(R.id.code)
    Mycode code;
    @InjectView(R.id.userphone)
    TextView userphone;
    private String vcode;
    private String intoPhone;
    private SpUtils sp;

    private Context context;

    @Override
    protected int getActivityLayout() {
        return R.layout.activity_psd_code;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        sp = new SpUtils();
        vcode = getIntent().getStringExtra("vcode");
        intoPhone = getIntent().getStringExtra("intoPhone");
        userphone.setText(intoPhone);
        Log.i("------", vcode);
        ToastUtils.showToast(PsdInputCodeActivity.this, vcode + "", 0);

        context = this;
    }

    @Override
    protected void onResume() {
        Log.i("----", "onResume()");
        super.onResume();
        initData();
    }

    private void initData() {
        code.getCodeView(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        code.getCodeView(this);
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.my_back, R.id.psd_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_back:
                finish();
                break;
        }
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("验证码错误，请重新输入");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                code.clearCode();
            }
        });
        builder.setCancelable(false);
        builder.show();
    }

    public void getCodeCount(int count) {
        Log.i("---", count + "getCodeCount");
        if (count == 4) {
            String netUrl = sp.getString(PsdInputCodeActivity.this, Constant.NET_URL, "");
            String username = sp.getString(PsdInputCodeActivity.this, Constant.ACCOUNT, "");
            String deviceId = sp.getString(PsdInputCodeActivity.this, Constant.DEVICEID, "");
            String spLan = SpUtils.getString(getApplicationContext(), SpUtils.KEY_LANGUAGE, "简体中文");
            String language = (String) SuperApp.lanMap.get(spLan)[0];
            String base64username = BaseUtil.getBase64(username);
            String code = this.code.getVerificationCode();
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("loginName", base64username);
            map.put("uid", deviceId);
            map.put("vcode", code);
            map.put("language", language);
            String requestUrl = netUrl + Constant.INTVCODEURL;

            OkGo.post(requestUrl).tag(this).params(map)
                    .execute(new AppCallback<VcodeReturnBean>(context) {
                        @Override
                        public void onSuccess(VcodeReturnBean resObj, Call call, Response response) {
                            if (resObj.success) {
                                Intent intent = new Intent(PsdInputCodeActivity.this, PsdSetLoginActivity.class);
                                intent.putExtra("vcode", vcode);
                                startActivity(intent);
                            } else {
                                showAlertDialog();
                            }
                        }

                        @Override
                        public VcodeReturnBean convertSuccess(Response response) throws Exception {
                            VcodeReturnBean resObj = new Gson().fromJson(response.body().string(), VcodeReturnBean.class);
                            return resObj;
                        }
                    });

        }
    }
}
